package cn.iocoder.yudao.module.member.controller.admin.message;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.member.controller.admin.jobView.vo.*;
import cn.iocoder.yudao.module.member.convert.jobView.JobViewConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.jobView.JobViewDO;
import cn.iocoder.yudao.module.member.service.jobView.JobViewService;
import cn.iocoder.yudao.module.system.api.message.MessageApi;
import cn.iocoder.yudao.module.system.api.message.MessageTypeApi;
import cn.iocoder.yudao.module.system.api.message.dto.*;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 -运营端- 站内信")
@RestController
@RequestMapping("/member/message")
@Validated
public class MemberMessageController {

    @Resource
    private MessageApi messageApi;
    @Resource
    private MessageTypeApi messageTypeApi;
    @Resource
    private AdminUserApi adminUserApi;
    @PostMapping("/get")
    @ApiOperation("获得站内信留言")
    public CommonResult<MessageRespDTO> getMessage(@RequestBody MessagePageReqDTO messagePageReqDTO) {

        MessageRespDTO messageRespDTO=messageApi.getMessage(messagePageReqDTO.getId());
        MessageTypeRespDTO messageTypeRespDTO=messageTypeApi.getMessageType(messageRespDTO.getType());
        if(messageTypeRespDTO !=null){
            messageRespDTO.setTypeName(messageTypeRespDTO.getName());
        }else{
            messageRespDTO.setTypeName("类型已删除");
        }
        MessageUpadateReqDTO messageUpadateReqDTO=new MessageUpadateReqDTO();
        BeanUtils.copyProperties(messageRespDTO,messageUpadateReqDTO);

        if(messageRespDTO.getStatus().equals("0")||messageRespDTO.getStatus().equals("4")){
            messageUpadateReqDTO.setStatus("1");
            messageApi.updateMessage(messageUpadateReqDTO);
        }
        return success(messageRespDTO);
    }

    @PostMapping("/page")
    @ApiOperation("获得站内信留言分页")
    public CommonResult<PageResult<MessageRespDTO>> getMessagePage(@RequestBody MessagePageReqDTO messagePageReqDTO) {
        messagePageReqDTO.setLeaveType(1);
        return success(messageApi.getMessagePage(messagePageReqDTO));
    }


    @PostMapping("/replyMessage")
    @ApiOperation("站内信留言回复")
    public CommonResult<Boolean> replyMessage(@RequestBody MessageInfoCreateReqDTO messageInfoCreateReqDTO) {
        MessageRespDTO messageRespDTO= messageApi.getMessage(messageInfoCreateReqDTO.getMessageId());
        AdminUserRespDTO adminUserRespDTO=adminUserApi.getUser(SecurityFrameworkUtils.getLoginUserId());
        messageInfoCreateReqDTO.setReply(adminUserRespDTO.getNickname());
        messageInfoCreateReqDTO.setReplyType(0);
        messageApi.replyMessage(messageInfoCreateReqDTO);
        MessageUpadateReqDTO messageUpadateReqDTO=new MessageUpadateReqDTO();
        BeanUtils.copyProperties(messageRespDTO,messageUpadateReqDTO);
        messageUpadateReqDTO.setStatus("2");
        messageUpadateReqDTO.setReply(SecurityFrameworkUtils.getLoginUser().getUsername());
        messageUpadateReqDTO.setReplyTime(new Date());
        messageApi.updateMessage(messageUpadateReqDTO);
        return success(true);
    }


    @PostMapping("/changeMessageStatus")
    @ApiOperation("修改站内信已读")
    public CommonResult<Boolean> changeMessageStatus(@RequestBody MessageUpadateReqDTO messageUpadateReqDTO) {
        messageApi.changeMessageStatus(messageUpadateReqDTO.getIds(),messageUpadateReqDTO.getStatus(),0);
        return success(true);
    }

    @PostMapping("/allchangeMessageStatus")
    @ApiOperation("修改站内信全部已读")
    public CommonResult<Boolean> allchangeMessageStatus(@RequestBody MessageUpadateReqDTO messageUpadateReqDTO) {
        messageApi.allchangeMessageStatus(messageUpadateReqDTO.getStatus(),0,null,SecurityFrameworkUtils.getLoginUserId());
        return success(true);
    }

}
